import 'package:color_dart/RgbaColor.dart';
import 'package:flutter/material.dart';
import 'package:im_flutter_sdk_example/components/empty_list.dart';
import 'package:im_flutter_sdk_example/provider/tag_model.dart';
import 'package:im_flutter_sdk_example/utils/custom_appbar.dart';
import 'package:im_flutter_sdk_example/utils/event_bus.dart';
import 'package:im_flutter_sdk_example/utils/global.dart';
import 'package:provider/provider.dart';
import 'package:quiver/strings.dart';

class SelectTags extends StatefulWidget {
  SelectTags({Key key}) : super(key: key);

  @override
  _ChipDemoState createState() => _ChipDemoState();
}

class _ChipDemoState extends State<SelectTags> {
  FocusNode _focusNode;
  TextEditingController _textcontroller;
  void _loadUserTags() async {
    TagProvider tagProvider = Provider.of<TagProvider>(context);
    tagProvider.loadMyTags();
  }

  @override
  void initState() {
    super.initState();
    _focusNode = new FocusNode();
    //请求我的兴趣
    Future.delayed(Duration.zero, () {
      _loadUserTags();
    });
  }

  @override
  Widget build(BuildContext context) {
    TagProvider tagProvider = Provider.of<TagProvider>(context);
    if (tagProvider == null) return EmptyList();
    if (tagProvider.data == null) return EmptyList();

    return Scaffold(
      appBar: customAppbar(
        title: '我的标签',
        context: context,
        // relaunch: true,
      ),
      backgroundColor: rgba(255, 255, 255, 1),
      body: SafeArea(
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: SingleChildScrollView(
            child: Column(
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Container(
                      height: 55,
                      width: G.screenWidth() * 0.6,
                      padding: EdgeInsets.symmetric(horizontal: 5),
                      decoration: BoxDecoration(border: G.borderBottom()),
                      child: TextField(
                        controller: _textcontroller = TextEditingController(),
                        keyboardType: TextInputType.text,
                        decoration: InputDecoration(
                            counterText: "",
                            border: InputBorder.none,
                            hintText: '输入感兴趣的领域',
                            hintStyle: TextStyle(
                              color: rgba(163, 163, 163, 1),
                              fontSize: 16,
                            )),
                        onSubmitted: (e) {
                          if (isNotBlank(e)) {
                            _addUserTags([e.trim()]);
                            _textcontroller?.clear();
                          }
                        },
                      ),
                    ),
                    SizedBox(
                      width: G.screenWidth() * 0.3,
                      child: RaisedButton(
                        color: Colors.blue,
                        highlightColor: Colors.blue[400],
                        colorBrightness: Brightness.dark,
                        splashColor: Colors.grey,
                        onPressed: () {
                          String text = _textcontroller.text;
                          if (isNotBlank(text)) {
                            _addUserTags([text.trim()]);
                            FocusScopeNode currentFocus =
                                FocusScope.of(context);
                            currentFocus.unfocus();
                            // eventBus.fire(NewsEvent());
                          }
                        },
                        child: Text('添加'),
                      ),
                    )
                  ],
                ),
                Container(
                  margin: EdgeInsets.only(top: 25),
                  child: Wrap(
                    spacing: 25.0,
                    runSpacing: 4.0,
                    alignment: WrapAlignment.start,
                    children: tagProvider.data.tags.map<Widget>((tag) {
                      return Chip(
                        backgroundColor: rgba(233, 242, 254, 1),
                        deleteIconColor: rgba(25, 109, 236, 1),
                        labelStyle: TextStyle(
                            color: rgba(25, 109, 236, 1), fontSize: 14),
                        shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(4)),
                        label: Text('${tag.label}'),
                        onDeleted: () {
                          setState(() {
                            tagProvider.delTag(tag.label);
                            eventBus.fire(NewsEvent());
                          });
                        },
                      );
                    }).toList(),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  dispose() {
    super.dispose();
    _focusNode.unfocus();
  }

  void _addUserTags(List<String> tags) async {
    // 去除 空字符
    var res = await G.req.user.addUserTag(tagIds: tags);
    _loadUserTags();
  }
}
